Skip to content

Conversation

@william-tang914
Copy link
Contributor

This change refactors the FlexCAN driver to use per-instance message buffer configuration instead of global Kconfig options. The driver now calculates message buffer allocation at compile time based on device tree properties for each FlexCAN instance.

Key changes:

  • Remove global CAN_MAX_MB Kconfig option and associated range constraint
  • Add max_mb, rx_mb, and tx_mb fields to mcux_flexcan_config structure
  • Move callback arrays and atomic allocation bitmaps from static global arrays to per-instance static arrays generated by macros
  • Update data structure to use pointers to per-instance arrays instead of fixed-size embedded arrays
  • Add compile-time validation to ensure sufficient message buffers are available for required RX filters

This approach allows different FlexCAN instances to have different message buffer configurations based on their hardware capabilities and device tree settings, improving memory efficiency and flexibility. The change maintains backward compatibility while enabling better resource utilization across different SoC variants.

Fixes #92798

@william-tang914 william-tang914 force-pushed the feature/move-to-per-instance-message-buffer-configuration branch from 9e4b937 to d8b2c9b Compare November 17, 2025 05:59
Fix various formatting issues in NXP device tree files including:
- Inconsistent indentation in multi-line interrupt and pll-clocks arrays
- Missing spaces around assignment operators
- Inconsistent blank line spacing between device tree nodes

Also update copyright header in nxp_k6x.dtsi.

Signed-off-by: William Tang <william.tang@nxp.com>
Add the `nxp,max-mb` and `nxp,max-mb-fd` device tree property to
all NXP FlexCAN controller nodes across various SoC families to
specify the maximum number of 8-byte and 64-byte payload message
buffers supported by each FlexCAN instance.

This change updates device tree source files for multiple NXP SoC
families including Kinetis K6x, RT10xx, RT11xx, RT118x, MCX, S32K,
S32Z series, and i.MX8MP/i.MX93. The property values are set based
on hardware specifications for each specific FlexCAN instance.

This property addition ensures proper resource allocation and
prevents buffer overflow issues in FlexCAN driver implementations.

Signed-off-by: William Tang <william.tang@nxp.com>
[FLEXCAN_INST_RX_MB(id)] = {0}; \
\
static struct mcux_flexcan_tx_callback flexcan_tx_cbs_##id \
[FLEXCAN_INST_TX_MB(id)] = {0}; \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just query, do we need to consider the case that FLEXCAN_INST_RX_MB(id) or FLEXCAN_INST_TX_MB(id) is 0?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In previous, Kconfig symbol CAN_MAX_FILTER is confined to be greater than 0, but there was no constraint that MCUX_FLEXCAN_MAX_TX not be 0.
If MCUX_FLEXCAN_MAX_TX is 0, compiler will report following warning

zephyrproject/zephyr/drivers/can/can_mcux_flexcan.c:1041:32: warning: array subscript 'alloc' is outside the bounds of an interior zero-length array 'struct mcux_flexcan_tx_callback[0]' [-Wzero-length-bounds]
 1041 |         function = data->tx_cbs[alloc].function;

For safety, I think it would be better to confine FLEXCAN_INST_TX_MB(id) and FLEXCAN_INST_RX_MB(id) greater than 0.

This change refactors the FlexCAN driver to use per-instance message
buffer configuration instead of global Kconfig options. The driver now
calculates message buffer allocation at compile time based on device
tree properties for each FlexCAN instance.

Key changes:
- Remove global CAN_MAX_MB Kconfig option and associated range constraint
- Add max_mb, rx_mb, and tx_mb fields to mcux_flexcan_config structure
- Move callback arrays and atomic allocation bitmaps from static global
  arrays to per-instance static arrays generated by macros
- Update data structure to use pointers to per-instance arrays instead
  of fixed-size embedded arrays
- Add compile-time validation to ensure sufficient message buffers are
  available for required RX filters and RX filters greater than 0

This approach allows different FlexCAN instances to have different
message buffer configurations based on their hardware capabilities
and device tree settings, improving memory efficiency and flexibility.
The change maintains backward compatibility while enabling better
resource utilization across different SoC variants.

Fixes zephyrproject-rtos#92798

Signed-off-by: William Tang <william.tang@nxp.com>
@william-tang914 william-tang914 force-pushed the feature/move-to-per-instance-message-buffer-configuration branch from 205defb to ee6a72b Compare November 21, 2025 04:02
@sonarqubecloud
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

can: can_mcux_flexcan.c allows a mailbox overrun resulting in an imprecise data bus fault

4 participants